#include "teiheader.h"
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QStringList>

TeiHeader::TeiHeader(QString teiheaderfile)
{
    teifilename= teiheaderfile;
    QString fileDesc = this->getFileDescString(teiheaderfile);
    html = fileDescToHtml(fileDesc);
}

QString TeiHeader::getFileDescString(QString file)
{
    QString teiheader;
    QFile tei(file);
    if(!tei.open(QIODevice::ReadOnly | QIODevice::Text))
        return "";
    QDomDocument doc("tei");
    doc.setContent(&tei);
    QDomElement docElem = doc.documentElement();
    QDomNode n = docElem.firstChild();
    while(!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(!e.isNull()) {
            if(e.tagName() == "teiHeader") {
                QDomNode fileDesc = n.namedItem("fileDesc");
                QTextStream stream(&teiheader);
                fileDesc.save(stream, 0);
            }

        }
        n = n.nextSibling();
    }
    return teiheader;
}

//what I will processed is titleStmt/editionStmt/publicationStmt
QString TeiHeader::fileDescToHtml(QString fileDesc)
{

    QString title_string;
    QStringList editor_string_list;
    QStringList principal_string_list;
    QStringList funder_string_list;

    QString edition_string;
    QStringList resp_string_list;
    QString publisher_string;

    QString date_string;
    QString availability_string;
    //printf("%s\n", fileDesc.toLatin1().constData());

    QDomDocument doc("fileDesc");
    doc.setContent(fileDesc);
    QDomElement docElem = doc.documentElement();
    QDomNode n = docElem.firstChild();
    while(!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(!e.isNull() && e.tagName() == "titleStmt") {
            QDomNode titleN = e.firstChild();
            while(!titleN.isNull()) {
                QDomElement titleE = titleN.toElement();
                if(!titleE.isNull()) {
                    if(titleE.tagName() == "title") {
                        title_string += titleE.text() + ", ";
                    }
                    if(titleE.tagName() == "editor") {
                        editor_string_list << titleE.text();
                    }
                    if(titleE.tagName() == "principal") {
                        principal_string_list << titleE.text();
                    }
                    if(titleE.tagName() == "funder") {
                        funder_string_list  << titleE.text();
                    }
                }
                titleN = titleN.nextSibling();
            }

        }
        if(!e.isNull() && e.tagName() == "editionStmt") {
            QDomNode editionN = e.firstChild();
            while(!editionN.isNull()) {
                QDomElement editionE = editionN.toElement();
                if(!editionE.isNull()) {
                    if(editionE.tagName() == "edition") {
                        edition_string += editionE.toElement().text();
                    }
                    if(editionE.tagName() == "respStmt") {
                        QDomNode respN = editionE.namedItem("resp");
                        QDomNode nameN = editionE.namedItem("name");
                        QString resp = respN.toElement().text() + " by " + nameN.toElement().text();
                        resp_string_list<<resp;
                    }
                }
                editionN = editionN.nextSibling();
            }

        }
        if(!e.isNull() && e.tagName() == "publicationStmt") {
            QDomNode pubN = e.firstChild();
            while(!pubN.isNull()) {
                QDomElement pubE = pubN.toElement();
                if(!pubE.isNull()) {
                    if(pubE.tagName() == "publisher") {
                        publisher_string += "<br>\"" + pubE.text() + "\" ";
                    }
                    if(pubE.tagName() == "pubPlace") {
                        publisher_string += pubE.text() + " ";
                    }
                    if(pubE.tagName() == "date") {
                        date_string += pubE.text();
                    }
                    if(pubE.tagName() == "availability") {
                        QTextStream availstream(&availability_string);
                        pubN.save(availstream, 0);
                        availability_string = availability_string.replace("<ptr target=\"","")
                                .replace("\"/>","")
                                .replace("<p>","")
                                .replace("</p>","");
                    }
                }
                pubN = pubN.nextSibling();
            }

        }
        n = n.nextSibling();
    }


    QString html;

    html += "<b>Title : </b>" + title_string + "<br>";

    html += "<b>Editor : </b>";
    for(int i = 0 ; i < editor_string_list.size(); i++) {
            html += editor_string_list.at(i);
            if(i != editor_string_list.size() -1)
                html += ", ";
    }
    html +="<br>";

    html += "<b>Principal : </b>";
    for(int i = 0 ; i < principal_string_list.size(); i++) {
            html += principal_string_list.at(i);
            if(i != principal_string_list.size() -1)
                html += ", ";
    }
    html +="<br>";

    html += "<b>Funder : </b>";
    for(int i = 0 ; i < funder_string_list.size(); i++) {
            html += funder_string_list.at(i);
            if(i != funder_string_list.size() -1)
                html += ", ";
    }
    html +="<br><br>";


    html += "<b>Edition : </b>" + edition_string + "<br>";
    html += "<b>Responsibility : </b><br>";
    for(int i = 0; i < resp_string_list.size(); i++) {
        html += resp_string_list.at(i) +"<br>";
    }
    html += "<br>";
    html += "<b>Publisher Info : </b>" + publisher_string + "<br>";
    html += "<b>Date:</b>" + date_string + "<br>";
    html += "<b>Availability : </b><br>" + availability_string + "<br>";

    return html;

}

QString TeiHeader::toHtml()
{
    return html;
}
